package com.chen.stopwatch.lap_view;

import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.chen.stopwatch.R;

public class LapListView {
	protected Activity mContext = null;

	protected ListView mLapListView = null;

	protected LapDataAdapter mLapDataAdapter = null;

	protected LapDataProvider mLapDataProvider = null;

	protected ColorSelectorView mColorSelectorView = null;

	public LapListView(Activity context) {
		mContext = context;
		init();
	}

	public void destroy(boolean rotate) {
		mContext = null;
		mLapListView = null;
		if (null != mLapDataAdapter) {
			mLapDataAdapter.destroy();
			mLapDataAdapter = null;
		}

		if (null != mLapDataProvider && !rotate) {
			mLapDataProvider.destroy();
			mLapDataProvider = null;
		}

		if (null != mColorSelectorView) {
			mColorSelectorView.destroy();
			mColorSelectorView = null;
		}
	}

	protected void init() {
		initListView();
		if (null == mLapListView)
			return;

		mLapDataProvider = new LapDataProvider();

		mLapDataAdapter = new LapDataAdapter(mContext);
		mLapDataAdapter.setDataProvider(mLapDataProvider);

		mLapListView.setAdapter(mLapDataAdapter);
		mLapListView.setOnCreateContextMenuListener(mOnCreateContextMenuListener);
		mColorSelectorView = new ColorSelectorView(mContext);
		mColorSelectorView.setColorSelectListener(mIColorChangedListener);
	}

	protected void initListView() {
		mLapListView = (ListView) mContext.findViewById(R.id.lap_view);
	}

	public LapDataProvider getLapDataProvider() {
		return mLapDataProvider;
	}

	public void setLapDataProvider(LapDataProvider dataProvider) {
		if (null != mLapDataProvider) {
			mLapDataProvider.destroy();
			mLapDataProvider = null;
		}

		mLapDataProvider = dataProvider;
		if (null != mLapDataAdapter) {
			mLapDataAdapter.setDataProvider(mLapDataProvider);
			refresh();
		}
	}

	private final IColorChangedListener mIColorChangedListener = new IColorChangedListener() {
		public void notify(int color) {
			if (null != mLapDataAdapter) {
				mLapDataProvider.setColor(mClickedPosition, color);
				mLapDataAdapter.notifyDataSetChanged();
			}
			if (null != mColorSelectorView) {
				mColorSelectorView.hide();
			}
		}
	};

	private static final int LAP_ITEM_SET_COLOR = 0;

	private static final int LAP_ITEM_DELETE = 1;

	protected int mClickedPosition = -1;

	private final OnCreateContextMenuListener mOnCreateContextMenuListener = new OnCreateContextMenuListener() {

		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			try {
				AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
				mClickedPosition = mi.position;
				// menu.add(0, LAP_ITEM_SET_COLOR, 0,
				// R.string.lap_view_set_color)
				// .setOnMenuItemClickListener(mOnMenuItemClickListener);
				// menu.add(0, LAP_ITEM_DELETE, 1, R.string.delete)
				// .setOnMenuItemClickListener(mOnMenuItemClickListener);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	private final OnMenuItemClickListener mOnMenuItemClickListener = new OnMenuItemClickListener() {
		public boolean onMenuItemClick(MenuItem item) {
			return menuItemClick(item);
		}
	};

	private boolean menuItemClick(MenuItem item) {
		if (LAP_ITEM_SET_COLOR == item.getItemId()) {
			// mLapDataProvider.setColor(mClickedPosition,
			// mContext.getResources()
			// .getColor(R.color.red));
			mColorSelectorView.show();
		} else if (LAP_ITEM_DELETE == item.getItemId()) {

		}
		return true;
	}

	public void lap(long l, long m) {
		if (null == mLapDataProvider)
			return;
		mLapDataProvider.add(l, m);

		refresh();
	}

	public void delete() {

	}

	public String getShareText() {
		if (null == mLapDataAdapter)
			return "";
		return mLapDataAdapter.getText();
	}

	public String getStartTime() {
		if (null == mLapDataProvider)
			return "";
		return mLapDataProvider.getInsertTime();
	}

	protected void refresh() {
		if (null != mLapDataAdapter)
			mLapDataAdapter.notifyDataSetChanged();
		if (null != mLapListView && 0 != mLapListView.getFirstVisiblePosition()) {
			mLapListView.setSelection(0);
		}
	}

	public void clear() {
		if (null == mLapDataProvider)
			return;
		mLapDataProvider.clear();

		refresh();
	}
}
